#include "LineEditDelegate.h"
#include <QRegExp>
#include <QRegExpValidator>
#include <QPainter>
#include <QDebug>

LineEditDelegate::LineEditDelegate(QObject *parent)
{

}

LineEditDelegate::~LineEditDelegate()
{

}

QWidget *LineEditDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    //返回该控件
    QLineEdit *editor = new QLineEdit(parent);
    return editor;
}

void LineEditDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    //返回该索引的模型，继而返回该模型中此索引的编辑角色数据
    QString value = index.model()->data(index, Qt::EditRole).toString();
    //给控件赋值
    QLineEdit *lineedit = static_cast<QLineEdit*>(editor);

    QRegExp regx(lineeditConditions);
    //QRegExp regx("^[0-9]+(.[0-9]{1,3})?$");
    QValidator *validator = new QRegExpValidator(regx, lineedit );
    lineedit->setValidator( validator ); 
    lineedit->setText(value);
}

void LineEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QLineEdit *lineedit = static_cast<QLineEdit*>(editor);
    QString value = lineedit->text();


    //设置模型的数据
    model->setData(index, value, Qt::EditRole);
}

void LineEditDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    //根据option,设置编辑框位置
    editor->setGeometry(option.rect);
}

void LineEditDelegate::getlineeditconditions(QString conditions)
{
    lineeditConditions = conditions;
}

